package com.vframework.system.menu.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.util.StringUtil;
import com.vframework.base.exception.BaseException;
import com.vframework.base.service.impl.BaseServiceImpl;
import com.vframework.system.menu.mapper.MenuMapper;
import com.vframework.system.menu.model.MenuBean;
import com.vframework.system.menu.model.OrgMenuBean;
import com.vframework.system.menu.model.RoleMenuBean;
import com.vframework.system.menu.service.MenuService;
import com.vframework.system.org.mapper.OrgMapper;
import com.vframework.system.org.model.OrgBean;
import com.vframework.system.role.mapper.RoleMapper;
import com.vframework.system.role.model.RoleBean;

@Service
public class MenuServiceImpl extends BaseServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private OrgMapper orgMapper;

    public MenuBean getMenuById(String menuId) throws BaseException {
        MenuBean menuBean = menuMapper.getMenuById(menuId);
        if (menuBean != null && menuBean.getOrgIds() != null && menuBean.getOrgIds().size() > 0) {
            String[] orgIds = menuBean.getOrgIds().toArray(new String[menuBean.getOrgIds().size()]);
            menuBean.setSiIds(orgIds);
        }
        return menuBean;
    }

    public List<MenuBean> getMenuList(Map<String, Object> paramMap) throws BaseException {
        return menuMapper.getMenuList(paramMap);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void addMenu(MenuBean menuBean) throws BaseException {
        Map<String, String> map = new HashMap<String, String>();
        map.put("cols", "menu_id");
        map.put("size", "6");
        map.put("tableName", "sys_menu");
        menuBean.setMenuId(menuMapper.getMaxIdLpad(map));
        if ("2".equals(menuBean.getMenuLevel()))
            menuBean.setMenuUse("F");
        else
            menuBean.setMenuUse("D");
        menuMapper.insertMenu(menuBean);
        dealMenu(menuBean);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void editMenu(MenuBean menuBean) throws BaseException {
        menuMapper.updateMenu(menuBean);
        dealMenu(menuBean);
    }

    private void dealMenu(MenuBean menuBean) throws BaseException {
        //查询所有机构
        List<OrgBean> orgBeans = orgMapper.getOrgList(new HashMap<String, Object>());
        for (OrgBean orgBean : orgBeans) {
            if ("0".equals(orgBean.getOrgType())) {
                //如果系统顶级机构没有被选中,则添加
                if (menuBean.getSiIds() == null || !Arrays.asList(menuBean.getSiIds()).contains(orgBean.getOrgId())) {
                    List<String> tempList = menuBean.getSiIds() == null ? new ArrayList<String>() : Arrays.asList(menuBean.getSiIds());
                    tempList.add(orgBean.getOrgId());
                    String[] tempArr = new String[tempList.size()];
                    tempList.toArray(tempArr);
                    menuBean.setSiIds(tempArr);
                }
            }
        }
        //删除org_menu
        menuMapper.deleteOrgMenu(menuBean.getMenuId());
        if (menuBean.getSiIds() != null) {
            List<OrgMenuBean> orgMenuList = new ArrayList<OrgMenuBean>();
            //插入sys_org_menu
            for (String si : menuBean.getSiIds()) {
                OrgMenuBean orgMenuBean = new OrgMenuBean();
                orgMenuBean.setMenuId(menuBean.getMenuId());
                orgMenuBean.setOrgId(si);
                orgMenuList.add(orgMenuBean);
            }
            //插入org_menu
            menuMapper.insertOrgMenu(orgMenuList);
            //role_menu中roleids
            List<String> roleIds = menuMapper.getRoleMenuByMenuId(menuBean.getMenuId());
            
            //删除非选中的role_menu
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("menuId", menuBean.getMenuId());
            paramMap.put("ids", menuBean.getSiIds());
            //删除role_menu
            menuMapper.deleteRoleMenuByOrgs(paramMap);
            //查询roleList
            paramMap = new HashMap<String, Object>();
            paramMap.put("param_siIds", menuBean.getSiIds());
            List<RoleBean> roleList = roleMapper.getRoleList(paramMap);
            Iterator<RoleBean> it = roleList.iterator();
            List<RoleMenuBean> roleMenuList = new ArrayList<RoleMenuBean>();
            while(it.hasNext()) {
                RoleMenuBean roleMenuBean = new RoleMenuBean();
                RoleBean role = it.next();
                //删除非系统和机构管理员角色和role_menu表中存在的角色
                if (StringUtil.isEmpty(role.getRoleType()) || Integer.parseInt(role.getRoleType()) > 1 || roleIds.contains(role.getRoleId())) {
                    it.remove();
                } else {
                    roleMenuBean.setRoleId(role.getRoleId());
                    roleMenuBean.setMenuId(menuBean.getMenuId());
                    roleMenuList.add(roleMenuBean);
                }
            }
            //插入role_menu
            if (roleMenuList != null && roleMenuList.size() > 0) {
                menuMapper.insertSysRoleMenu(roleMenuList);
            }
        }
    }
    
    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteMenu(Map<String, Object> map) throws BaseException {
        String[] ids = (String[]) map.get("ids");
        if (ids != null) {
            menuMapper.deleteOrgMenuChildren(map);
            menuMapper.deleteRoleMenu(map);
            menuMapper.deleteMenu(map);
        }
    }

    public List<MenuBean> getPmenuList(String menuLevel) throws BaseException {
        return menuMapper.getPmenuList(menuLevel);
    }
}
